// pages/facePay/facePay.js
const app=getApp();
Page({
  /** 页面的初始数据*/
  data: {
    inputMoney:0,
    preafterprice:0,
    checked1: true,
    coupon: {},
    points: {},
    integral: 0,
    integralpri: 0,
    yhq: {
      id: null,
      name: '不使用优惠券',
      pri: 0
    },
    thankimg:'',
    shop:{
      name:'',
      img:''
    },
    orderdd: {},//提交的时候的订单
    bool:false//判断是否提交
  },
  intejral: function (e) {
    this.setData({
      checked1: !this.data.checked1
    })
    this.preafterprice();
  },
  /*jifen*/
  integral: function (e) {
    var that = this;
    var integral = 0;
    var integralpri = 0;
    if (!that.data.points.data) return false;
    var largepri = parseInt((that.data.points.data.num / that.data.points.data.use_rule) * 100) / 100;
    var pri = parseInt((that.data.inputMoney / that.data.points.data.use_rule) * 100) / 100;
    if (pri > largepri) {
      integral = that.data.points.data.num;
      integralpri = largepri
    } else {
      integral = that.data.inputMoney;
      integralpri = pri;
    }
    that.setData({
      integral: integral,
      integralpri: integralpri
    })
  },
  /* 选择优惠券*/
  chooseyh: function (e) {
    var that = this;
    if (that.data.coupon.data.length == 0) {
      wx.showToast({
        title: '您暂无可使用的优惠券',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else {
      wx.navigateTo({
        url: '../myYouhui/myYouhui?id=0',
      })
    }
  },
  /*优惠后的价格*/
  preafterprice: function () {
    var that = this;
    var order = {};
    var points = null;
    var yhqid = null;
    if (that.data.checked1) {
      points = that.data.integral;
    }
    if (parseInt(that.data.yhq.id) && parseInt(points)) {
      order = {
        price: that.data.inputMoney,
        true_price: that.data.preafterprice,
        coupon_id: parseInt(that.data.yhq.id),
        points: points,
      }
    } else if (parseInt(points)) {
      order = {
        price: that.data.inputMoney,
        true_price: that.data.preafterprice,
        points: points,
      }
    } else if (parseInt(that.data.yhq.id)) {
      order = {
        price: that.data.inputMoney,
        true_price: that.data.preafterprice,
        coupon_id: parseInt(that.data.yhq.id),
      }
    }else{
      order = {
        price: that.data.inputMoney,
        true_price: that.data.preafterprice,
      }
    }
    that.setData({
      orderdd: order
    })
    wx.request({
      url: app.globalData.directory + 'api/v2/pay/handle_price/face',
      method: 'POST',
      data: order,
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          that.setData({
            preafterprice: res.data.data,
            'orderdd.true_price': res.data.data
          })
          if (that.data.bool) {
            that.pay();
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  /*提交表单 */
  pay: function () {
    var that = this;
    wx.request({
      url: app.globalData.directory + 'api/v2/pay/faceorder',
      method: 'POST',
      data: that.data.orderdd,
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          if (res.data.msg == 'success') {
            wx.requestPayment({
              'timeStamp': res.data.data.timestamp,
              'nonceStr': res.data.data.nonceStr,
              'package': res.data.data.package,
              'signType': res.data.data.signType,
              'paySign': res.data.data.paySign,
              'success': function (res) {
              },
              'fail': function (res) {
                wx.showToast({
                  title: res.errMsg,
                  icon: 'none',
                  duration: 2000,
                })
              }
            })
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  /*提交表单 */
  bindsub: function () {
    if (!app.globalData.user.nickname) {
      wx.switchTab({
        url: '../my/my',
      })
      return false;
    }
    if (!this.data.inputMoney){
      wx:wx.showToast({
        title: '请输入金额',
        icon: 'none',
        duration: 2000,
      })
      return false;
    }
    this.setData({
      bool: true
    })
    this.preafterprice();
  },
  payMoney: function (e) {
    let inputMoney = e.detail.value;
    let selected = this.data.selected;
    if (inputMoney != this.data.inputMoney) {
      selected = false;
    }
    if (inputMoney < 0) {
      return 0
    }
    this.setData({
      inputMoney:parseFloat(inputMoney),
      selected: selected
    })
    this.integral();
  },
  /** 生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    this.setData({
      coupon: app.globalData.coupon,
      points: app.globalData.points
    })
    wx.request({
      url: app.globalData.directory + 'api/v2/getshopinfo',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          that.setData({
            'shop.img': res.data.data.img,
            'shop.name':res.data.data.name,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
    wx.request({
      url: app.globalData.directory + 'api/v2/banner/22',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          that.setData({
            thankimg:res.data.data.item[0].img
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
    if (parseInt(app.globalData.choosewelfare) == 0) {
      that.setData({
        'yhq.id': null,
        'yhq.name': '不使用优惠券',
        'yhq.pri': 0,
      })
      that.integral();
      that.preafterprice();
      return false;
    }
    if (parseInt(app.globalData.choosewelfare)) {
      that.data.coupon.data.forEach(function (item, index) {
        if (item.id == app.globalData.choosewelfare) {
          that.setData({
            'yhq.id': item.id,
            'yhq.name': item.name,
            'yhq.pri': item.amount_cash,
          })
        }
      })
    }
    this.integral();
    this.preafterprice();
  },
  /** 生命周期函数--监听页面显示*/
  onShow: function () {
    var that = this;
    if (parseInt(app.globalData.choosewelfare) == 0) {
      that.setData({
        'yhq.id': null,
        'yhq.name': '不使用优惠券',
        'yhq.pri': 0,
      })
      that.preafterprice();
      return false;
    }
    if (parseInt(app.globalData.choosewelfare)) {
      console.log(that.data.coupon.data);
      that.data.coupon.data.forEach(function (item, index) {
        if (item.id == app.globalData.choosewelfare) {
          that.setData({
            'yhq.id': item.id,
            'yhq.name': item.name,
            'yhq.pri': item.amount_cash,
          })
          that.preafterprice();
        }
      })
    }
  },
})